package bzqn.com.rongcloudsdk;

import com.alibaba.fastjson.JSON;

import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import bzqn.com.rongcloudsdk.mod.TokenMod;
import bzqn.com.rongcloudsdk.utils.UserInfoMod;

/**
 * Created by a on 2017/5/15.
 */

public class  ServerApi {
    public static String API_HOST = "http://www.diaojimao.com";
    public static String API_GET_TOKEN =API_HOST+ "/index.php?g=api&m=RongCloud&a=get_user_token";
    public static String API_GET_USERINFO = API_HOST + "/index.php?g=api&m=RongCloud&a=get_user_avatar";
    private static FinalHttp http;
    static {
        http = new FinalHttp();
    }
    public static void getUserinfo(final HttpResponseCallback<UserInfoMod> callback, String uid){
        if (callback==null)return;
        AjaxParams params = new AjaxParams();
        params.put("uid",uid);
        http.post(API_GET_USERINFO,params, new AjaxCallBack<String>() {
            @Override
            public void onStart() {
                super.onStart();
                callback.onStart();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                callback.onFaile(strMsg);
            }

            @Override
            public void onSuccess(String s) {
                super.onSuccess(s);
                UserInfoMod mod = JSON.parseObject(s,UserInfoMod.class);
                if (mod==null){
                    mod = new UserInfoMod();
                }
                callback.onSuccess(mod);
            }
        });
    }
    public static void getToken(final HttpResponseCallback<TokenMod> callback, String uid){
        if (callback==null)return;
        //获取token
        AjaxParams params = new AjaxParams();
        params.put("uid",uid);
        http.post(API_GET_TOKEN,params, new AjaxCallBack<String>() {
            @Override
            public void onStart() {
                super.onStart();
                callback.onStart();
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                callback.onFaile(strMsg);
            }

            @Override
            public void onSuccess(String s) {
                super.onSuccess(s);
                TokenMod mod = JSON.parseObject(s,TokenMod.class);
                if (mod==null){
                    mod = new TokenMod();
                }
                callback.onSuccess(mod);
            }
        });
    }
}
